/*****************************************************************************
 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Queues for different kinds of SDL events
 *//*
 * LEGAL:   COPYRIGHT (C) 2009 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#if COMPILE_SDL

#define GLUE_SDL_QUEUE_CC 1
#include <deque>
#include "base/module.hh"
using namespace base;
#include "glue/module.hh"
#include "glue/sdl_queue.hh"

namespace glue {

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////  SDLQueue  ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

SDLQueue* SDLQueue::msInstance;

/*****************************************************************************
 * ctor/dtor.
 *****************************************************************************/
SDLQueue::SDLQueue( void )
:   mJoystickQueue()
{
    // NOP
}

SDLQueue::~SDLQueue()
{
    // NOP
}

/*****************************************************************************
 * Dequeue all events directly from SDL's queue and divert them to specific queues.
 *****************************************************************************/
void
SDLQueue::Pulse( void )
{
    SDL_Event eventSDL; 
    while ( SDL_PollEvent( &eventSDL ) )
    {
        switch( eventSDL.type )
        {
            case SDL_JOYAXISMOTION:
            case SDL_JOYBUTTONUP:
            case SDL_JOYBUTTONDOWN:
            {
                mJoystickQueue.push_back( eventSDL );
            }
            break;

            default: // discard unused event
            break;
        }
    }
}

/*****************************************************************************
 * (private subroutine)
 * Dequeue an event from a higher-level queue.
 * @return False if no more events.
 *****************************************************************************/
bool
SDLQueue::DequeueEvent( SDL_Event& sdlEvent /*OUT*/, std::deque<SDL_Event>& queue )
{
    // Now is an opportunity to pulse the queue (dequeue from SDL's queue).
    Pulse();

    // Event enqueued?
    if ( queue.empty() )
    {
        return false;
    }
    else
    {
        sdlEvent = queue.front();
        queue.pop_front();
        return true;
    }
}

/*****************************************************************************
 * Dequeue a joystick event (from the higher-level queue).
 * @return False if no more joystick events.
 *****************************************************************************/
bool
SDLQueue::DequeueJoystickEvent( SDL_Event& sdlEvent /*OUT*/ )
{
    return DequeueEvent( sdlEvent, mJoystickQueue );
}

} // namespace glue

#endif // COMPILE_SDL
